package main

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

func main() {

	// 初始化 Gin 引擎
	r := gin.Default()

	r.LoadHTMLGlob("templates/**/*")
	r.Static("/static", "./static")

	// 定义路由
	r.GET("/admin", func(c *gin.Context) {
		c.HTML(200, "admin/index.html", gin.H{
			"title": "Main website",
			"data": map[string]interface{}{
				"name": "John",
				"age":  30,
			},
		})
	})
	r.GET("/admin/index", func(c *gin.Context) {
		c.HTML(200, "admin.html", gin.H{
			"title": "Main index",
			"data": map[string]interface{}{
				"name": "aaaa",
				"age":  30,
			},
		})
	})
	r.GET("/index", func(c *gin.Context) {
		c.HTML(200, "default/index.html", gin.H{
			"title": "index",
			"data": map[string]interface{}{
				"name": "index",
				"age":  30,
			},
		})
	})
	r.GET("/news", func(c *gin.Context) {
		c.HTML(200, "default/news.html", gin.H{
			"title": "index",
			"data": map[string]interface{}{
				"name": "index",
				"age":  30,
			},
		})
	})

	r.POST("/news", func(c *gin.Context) {
		file, _ := c.FormFile("file")
		name := c.PostForm("name")
		fmt.Println(file.Filename)
		fmt.Println(name)
		dst := "./static/" + file.Filename
		// 上传文件到指定的目录
		c.SaveUploadedFile(file, dst)
		c.String(200, "上传")
	})
	r.Run(":8080")
}
